package com.system.controller;

import com.system.pojo.Result;
import com.system.pojo.administrator;
import com.system.service.AdminService;
import com.system.utils.JwtUtil;
import com.system.utils.Md5Util;
import com.system.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Validated
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/register")
    public Result register(String name,@Pattern(regexp = "^\\S{5,16}$") String account, @Pattern(regexp = "^\\S{5,16}$") String password){
         administrator a =adminService.findByAdminAccount(account);
         if(a==null){
            adminService.register(name,account,password);
            return Result.success();
         }else{
            return Result.error("用户名已被占用");
         }
    }


    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String account, @Pattern(regexp = "^\\S{5,16}$") String password){
        administrator loginAdmin = adminService.findByAdminAccount(account);
        if(loginAdmin==null){
        return Result.error("账号错误");
        }
        if(Md5Util.getMD5String(password).equals(loginAdmin.getPassword())){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",loginAdmin.getAdministratorID());
            claims.put("account",loginAdmin.getAccount());
            String token = JwtUtil.genToken(claims);

            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    @GetMapping("/adminInfo")
    public Result<administrator> adminInfo(){
        Map<String,Object> map = ThreadLocalUtil.get();
        String adminAccount = (String) map.get("account") ;
        administrator admin = adminService.findByAdminAccount(adminAccount);
        return Result.success(admin);
    }

    @PutMapping("/update")
    public Result update(String name){
        adminService.update(name);
        return Result.success();
    }

    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String> params){
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)){
            return Result.error("缺少必要的参数");
        }

        Map<String,Object> map = ThreadLocalUtil.get();
        String account = (String) map.get("account");
        administrator loginAdmin = adminService.findByAdminAccount(account);
        if(!loginAdmin.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("缺少必要的参数");
        }

        if(!rePwd.equals(newPwd)){
            return Result.error("两次密码不相同");
        }
        adminService.updatePwd(newPwd);
        return Result.success();
    }

}


